package com.example.coding_problem.Fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.coding_problem.R;
import com.example.coding_problem.Road_condition_query_List;
import com.google.gson.Gson;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class Road_condition_query extends Fragment {
    private TextView tv_rq;
    private TextView tv_xq;
    private TextView tv_wd;
    private TextView tv_xdsd;
    private TextView tv_pm_25;
    private ImageView iv_refresh;
    private List<Road_condition_query_List>road_list=new ArrayList<>();
    private ListView road_lv;
    public Road_condition_query() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.road_condition_query_list, container, false);
        initView(view);
        initData(view);
        return view;
    }
    private interface BlogService{
        @GET("aircondition")
        Call<ResponseBody>getBlog();
    }
    private void initView(View view) {
        road_lv=(ListView)view.findViewById(R.id.road_lv);
    }
    private void initData(View view) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service=retrofit.create(BlogService .class);
        Call<ResponseBody>call=service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson=new Gson();
                try {
                    Road_condition_query_List carlist=gson.fromJson(response.body().string(),Road_condition_query_List.class);
                    road_list.add(carlist);
                    road_lv.setAdapter(new RoadAdapter());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    private class RoadAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return road_list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.road_condition_query,null);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("E");
            Date date1=new Date(System.currentTimeMillis());
            Date date=new Date(System.currentTimeMillis());
            TextView tv_rq = (TextView) view1.findViewById(R.id.tv_rq);
            TextView tv_xq = (TextView) view1.findViewById(R.id.tv_xq);
            TextView tv_wd = (TextView) view1.findViewById(R.id.tv_wd);
            TextView tv_xdsd = (TextView) view1.findViewById(R.id.tv_xdsd);
            TextView tv_pm_25 = (TextView) view1.findViewById(R.id.tv_pm_25);
            iv_refresh = (ImageView) view1.findViewById(R.id.iv_refresh);
            iv_refresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    road_list=new ArrayList<>();
                    initData(view);
                }
            });
            tv_xq.setText(simpleDateFormat1.format(date)+"");
            tv_rq.setText(simpleDateFormat.format(date1)+"");
            tv_wd.setText("温    度:  "+road_list.get(0).getWendu()+"°C");
            tv_xdsd.setText("相对湿度:  "+road_list.get(0).getShidu()+"%");
            tv_pm_25.setText("PM2.5:  "+road_list.get(0).get_$Pm25192()+"ug/m3");
            return view1;
        }
    }
}
